assing-test

A short description of the post.

DONG Fang (Regina) https://www.linkedin.com/in/reginadongf/ (School of Computing and Information Systems, Singapore Management University)https://scis.smu.edu.sg/
07-17-2021

Launch packages

packages = c('ggiraph', 'plotly','DT', 'patchwork', 
             'raster', 'sf','tmap', 'mapview','gifski',
             'tidyverse', 'mlr','lubridate')
for (p in packages){
  if(!require(p, character.only = T)){
    install.packages(p)
  }
  library(p, character.only = T)
}

Import data

car_ass <- read_csv("data/car-assignments.csv")
gps <- read_csv("data/gps.csv")
cc <- read_csv("data/cc_data.csv", locale = locale(encoding = "windows-1252"))
loyalty <- read_csv("data/loyalty_data.csv", locale = locale(encoding = "windows-1252"))

gps$Timestamp = mdy_hms(gps$Timestamp)
cc$timestamp = mdy_hm(cc$timestamp)
loyalty$timestamp = mdy(loyalty$timestamp)

car_ass$CarID = as.character(car_ass$CarID)
gps$id = as.character(gps$id)
cc$last4ccnum = as.character(cc$last4ccnum)

cc$day = day(cc$timestamp)
cc$hour = hour(cc$timestamp)
loyalty$day = day(loyalty$timestamp)
gps$day = as.factor(day(gps$Timestamp))
gps$hour = as.factor(hour(gps$Timestamp))

geo map

bgmap <- raster("data/Geospatial/MC2-tourist.tif")

Abila_st <- st_read(dsn = "data/Geospatial",layer = "Abila")
Reading layer `Abila' from data source 
  `D:\ReginaDong\DataViz_blog\_posts\2021-07-17-assing-test\data\Geospatial' 
  using driver `ESRI Shapefile'
Simple feature collection with 3290 features and 9 fields
Geometry type: LINESTRING
Dimension:     XY
Bounding box:  xmin: 24.82401 ymin: 36.04502 xmax: 24.90997 ymax: 36.09492
Geodetic CRS:  WGS 84
gps_sf <- st_as_sf(gps, 
                   coords = c("long", "lat"),
                   crs = 4326)

# Group by id and day
gps_path <- gps_sf %>%
  group_by(id, day) %>%
  summarize(m = mean(Timestamp), 
            do_union=FALSE) %>%
  st_cast("LINESTRING")

np = npts(gps_path, by_feature = T)
gps_path2 <- cbind(gps_path, np) %>%
  filter(np > 1) # exclude orphan coordinate records

# Group by day and hour
gps_hour <- gps_sf %>%
  group_by(day, hour) %>%
  summarise(m = mean(Timestamp),
            do_union = FALSE) %>%
  st_cast("LINESTRING")
tmap_mode("view")

m <- tm_shape(bgmap) +
  tm_rgb(bgmap, r = 1,g = 2,b = 3,
       alpha = NA,
       saturation = 1,
       interpolate = TRUE,
       max.value = 255) +
  tm_shape(gps_path2) +
  tm_lines(col = "day") 
  #tm_facets(by = "id")
m